﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCA.UI.Utils;
using Telerik.Windows.Controls.Calendar;

namespace Baosight.ZNH.ZMCA.UI.Controls
{
    /// <summary>
    /// 该控件为日期选择控件，用户可以按年或时间段选择日期。
    /// 通过时间段选择时间时，起始时间和结束时间都是通过月份来选择的
    /// </summary>
    public partial class AnalysisDatePickerMonth : UserControl
    {
        public AnalysisDatePickerMonth()
        {
            InitializeComponent();

            fromDateTimePicker.DisplayFormat = Telerik.Windows.Controls.DateTimePickerFormat.Short;
            toDateTimePicker.DisplayFormat = Telerik.Windows.Controls.DateTimePickerFormat.Short;
            fromDateTimePicker.InputMode = Telerik.Windows.Controls.InputMode.DatePicker;
            toDateTimePicker.InputMode = Telerik.Windows.Controls.InputMode.DatePicker;
            fromDateTimePicker.DateSelectionMode = DateSelectionMode.Year;
            toDateTimePicker.DateSelectionMode = DateSelectionMode.Month;

            baseOnYear.Checked += new RoutedEventHandler(baseOnYear_Checked);
            baseOnRange.Checked += new RoutedEventHandler(baseOnRange_Checked);

            this.SetValue(AnalysisDatePickerMonth.ValueProperty, DateTime.Now.Year.ToString());
        }

        private bool isReadonly = false;
        public bool IsReadonly
        {
            get { return isReadonly; }
            set
            {
                isReadonly = value;
                this.dpdButton.IsEnabled = !isReadonly;
            }
        }
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (baseOnYear.IsChecked==true)
            {
                if (fromDateTimePicker.SelectedValue == null)
                {
                    Common.Alert("请选择年份。");
                    return;
                }
                txtDatetime.Text = fromDateTimePicker.SelectedDate.Value.Year.ToString();
            }
            else
            {
                if (fromDateTimePicker.SelectedValue == null)
                {
                    Common.Alert("请选择开始日期。");
                    return;
                }
                if (toDateTimePicker.SelectedValue == null)
                {
                    Common.Alert("请选择截止日期。");
                    return;
                }
                txtDatetime.Text = fromDateTimePicker.SelectedDate.Value.ToString(Common.DateTimeFormatDateYear)
                    + "至" +
                    toDateTimePicker.SelectedDate.Value.ToString(Common.DateTimeFormatDateYear);
            }
            Value = txtDatetime.Text;
            dpdButton.IsOpen = false;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            dpdButton.IsOpen = false;
        }


        public void InitDateTimePickerByTxtDatetime()
        {
            if (txtDatetime.Text.Length == 4)
            {
                fromDateTimePicker.SelectedDate = fromDateTimePicker.DisplayDate = DateTime.Parse(txtDatetime.Text + "-01-01");
                toDateTimePicker.SelectedDate = toDateTimePicker.DisplayDate = DateTime.Parse(txtDatetime.Text + "-01-01");
            }
            else
            {
                fromDateTimePicker.SelectedDate = fromDateTimePicker.DisplayDate = DateTime.Parse(txtDatetime.Text.Substring(0, txtDatetime.Text.IndexOf("至")) + "-01");
                toDateTimePicker.SelectedDate = toDateTimePicker.DisplayDate = DateTime.Parse(txtDatetime.Text.Substring(txtDatetime.Text.IndexOf("至") + 1) + "-01");
            }
        }

        public static readonly DependencyProperty ValueProperty 
            = DependencyProperty.Register("Value", 
            typeof(string),
            typeof(AnalysisDatePickerMonth), 
            new PropertyMetadata("", new PropertyChangedCallback(OnValuePropertyChanged)));

        public string Value
        {
            get {
                //return txtDatetime.Text; 
                return (string)GetValue(AnalysisDatePickerMonth.ValueProperty); 
            }
            set
            {
                SetValue(AnalysisDatePickerMonth.ValueProperty, value);
                
                //txtDatetime.Text = value; 
            }
        }

        private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AnalysisDatePickerMonth ctl = d as AnalysisDatePickerMonth;
            ctl.txtDatetime.Text = d.GetValue(AnalysisDatePickerMonth.ValueProperty).ToString();
            ctl.InitDateTimePickerByTxtDatetime();
        }

        private void baseOnYear_Checked(object sender, RoutedEventArgs e)
        {
            fromDateTimePicker.DateSelectionMode = DateSelectionMode.Year;
            toDateTimePicker.Visibility = System.Windows.Visibility.Collapsed;
            tbToData.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void baseOnRange_Checked(object sender, RoutedEventArgs e)
        {
            fromDateTimePicker.DateSelectionMode = DateSelectionMode.Month;
            toDateTimePicker.Visibility = System.Windows.Visibility.Visible;
            tbToData.Visibility = System.Windows.Visibility.Visible;
        }


        public DateSelectionMode DateSelectionMode
        {
            get
            {
                return fromDateTimePicker.DateSelectionMode;
            }
        }

        /// <summary>
        /// 获得开始日期
        /// </summary>
        public DateTime FromDate
        {
            get
            {
                DateTime fromDate = new DateTime();


                if (txtDatetime.Text.Length == 4)
                {
                    fromDate = DateTime.Parse(txtDatetime.Text + "-01-01");
                }
                else
                {
                    fromDate = DateTime.Parse(txtDatetime.Text.Substring(0, txtDatetime.Text.IndexOf("至")));
                }

                return fromDate;
            }
        }

        /// <summary>
        /// 获得结束日期
        /// </summary>
        public DateTime ToDate
        {
            get
            {
                DateTime toDate = new DateTime();
                if (txtDatetime.Text.Length == 4)
                {
                    DateTime fromDate = FromDate;
                    toDate = fromDate.AddYears(1).AddDays(-1);
                }
                else
                {
                    DateTime dt = DateTime.Parse(txtDatetime.Text.Substring(txtDatetime.Text.IndexOf("至") + 1) + "-01");
                    toDate = dt.AddMonths(1).AddDays(-1);
                }
                return toDate;
            }
        }
    }
}
